
#include "device.h"
#include "modbus.h"
#include "sensor_device.h"

#define DF_LOOP_TM_MS 1
int device::init()
{
    m_slaveid = 0x01;
    char* ip_or_device="/dev/uart1";
    ctx = modbus_new_rtu(ip_or_device, 115200, 'N', 8, 1);
    modbus_set_slave(ctx,m_slaveid);
}

int p300device::read_press()
{
    uint16_t tab_rp_bits[32];
    int rc = modbus_read_input_registers(ctx, 30001, 0x01, tab_rp_bits);
    if (rc == -1 ) {
        printf("ERROR modbus_read_input_registers single (%d)\n", rc);
        printf("address = %d\n", 0);
    }

}
int p300device::read_param_value()
{
    uint16_t tab_rp_bits[32];
    int rc = modbus_read_registers(ctx, 0x00, 0x01, tab_rp_bits);
    if (rc == -1 ) {
        printf("ERROR modbus_read_bits single (%d)\n", rc);
        printf("address = %d\n", 0);
    }
}
int p300device::write_param_value()
{
  int rc = modbus_write_register(ctx, 0x01, 0x02);
    if(rc == -1 && errno == EMBBADDATA)
    {
        return -1;
    }
}



//=====================

shared_ptr<devicemgr_module>     devicemgr_module::pdevicemgr;
shared_ptr<devicemgr_module> devicemgr_module::getInstance()
{
    if(nullptr == pdevicemgr)
    {
        pdevicemgr.reset(new devicemgr_module());
    }
    return pdevicemgr;
}

void devicemgr_module::init()
{
    device* m_sensordevice = new sensor_device();
    m_moduleId  = moduleid(EMODULE_MODULE_DEVICE_BASE,1); 
    CModule_Mgr::getInstance()->reg_module(m_moduleId, pdevicemgr);
}
void devicemgr_module::start()
{
    CModule::start();

    CMsg_Ptr pMsg(new msg_start());
    this->put_msg(pMsg);
}
void devicemgr_module::handle_msg(CMsg_Ptr pMsg)
{
    switch(pMsg->m_type)
    {
        case EMSG_SYS_START:
        {
            m_loopWkTm.add_task(mstm(DF_LOOP_TM_MS),[=](){
                CMsg_Ptr pmsg(new msg_loop());
                this->put_msg(pmsg);
            }, 1);
        }
        break;
        case EMSG_LOOP:
        {
            //do something
           // handle_update();
            //循环定时器
            m_loopWkTm.add_task(mstm(DF_LOOP_TM_MS),[=](){
                CMsg_Ptr pmsg(new msg_loop());
                this->put_msg(pmsg);
            }, 1);
        }
        break;
    }
}
//扫描
void devicemgr_module::handle_update()
{
    int press = m_device1.read_press();
    

}


